package org.codersunit.radboid.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Calendar;

import org.codersunit.radboid.R;
import org.codersunit.radboid.data.Lecture;
import org.codersunit.radboid.data.Schedule;
import org.codersunit.radboid.data.Schedule.Period;
import org.codersunit.radboid.util.Tuple;

public class ScheduleAdapter extends BaseAdapter {
	private Schedule lectures;
	private Context context;
	private java.text.DateFormat timeFormat;
	protected final static int TRANSPARANCY_LEVEL = 0xAA000000;
	
	public ScheduleAdapter(Context ctx, Schedule schedule) {
		lectures = schedule;
		context = ctx;
		timeFormat = DateFormat.getTimeFormat(context);
	}

	public boolean areAllItemsEnabled() {
		return false;
	}
	
	@Override
	public int getCount() {
		return lectures.getFlatCount();
	}

	@Override
	public Tuple<Period, Integer> getItem(int position) {
		return lectures.getLocation(position);
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public boolean isHead(int position) {
		return lectures.getFlat(position) instanceof Calendar;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Tuple<Period, Integer> item = getItem(position);
		if(item.getSecond() < 0) {
			return getHeadView(item, convertView, parent);
		} else {
			return getChildView(item, convertView, parent);
		}
	}
	
	protected LayoutInflater getInflater() {
		return (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public View getHeadView(Tuple<Period, Integer> item, View convertView, ViewGroup parent) {
		View view = convertView;
		Calendar date = item.getFirst().getDate();
		if(convertView == null || convertView.getId() != R.id.list_group_header) {
			LayoutInflater inflater = getInflater();
			view = inflater.inflate(R.layout.list_group_header, parent, false);
		}
		bind(view, R.id.list_group_header_text, DateFormat.format("EEEE, MMMM dd", date));
		return view;
	}
	
	public View getChildView(Tuple<Period, Integer> item, View convertView, ViewGroup parent) {
		Lecture lecture = item.getFirst().getLectures().get(item.getSecond());
		if(item.getSecond() == 0) {
			if(item.getFirst().getLectures().size() == 1) {
				return getSingleChildView(lecture, convertView, parent);
			} else {
				return getFirstChildView(lecture, convertView, parent);
			}
		} else if(item.getSecond() == item.getFirst().getLectures().size() - 1) {
			return getLastChildView(lecture, convertView, parent);
		} else {
			return getMiddleChildView(lecture, convertView, parent);
		}
	}
	
	public View getMiddleChildView(Lecture lecture, View convertView, ViewGroup parent) {
		View view = convertView;
		if(convertView == null || convertView.getId() != R.id.list_child_middle) {
			LayoutInflater inflater = getInflater();
			view = inflater.inflate(R.layout.list_child_middle, parent, false);
		}
		
		String startTime = timeFormat.format(lecture.getStart().getTime());
		String endTime = timeFormat.format(lecture.getEnd().getTime());

		ImageView image = (ImageView)view.findViewById(R.id.list_child_subject_indicator);
		image.setColorFilter(getColorForType(lecture.getType()), Mode.SRC_OVER);
		bind(view, R.id.list_child_subject_name, lecture.getName());
		bind(view, R.id.list_child_subject_time, startTime + " - " + endTime);
		bind(view, R.id.list_child_subject_location, lecture.getLocation());
		view.setClickable(true);
		return view;
	}

	public View getLastChildView(Lecture lecture, View convertView,	ViewGroup parent) {
		View view = convertView;
		if(convertView == null || convertView.getId() != R.id.list_child_last) {
			LayoutInflater inflater = getInflater();
			view = inflater.inflate(R.layout.list_child_last, parent, false);
		}
		
		String startTime = timeFormat.format(lecture.getStart().getTime());
		String endTime = timeFormat.format(lecture.getEnd().getTime());

		ImageView image = (ImageView)view.findViewById(R.id.list_child_subject_indicator);
		image.setColorFilter(getColorForType(lecture.getType()), Mode.SRC_OVER);
		bind(view, R.id.list_child_subject_name, lecture.getName());
		bind(view, R.id.list_child_subject_time, startTime + " - " + endTime);
		bind(view, R.id.list_child_subject_location, lecture.getLocation());
		view.setClickable(true);
		return view;
	}
	
	public View getFirstChildView(Lecture lecture, View convertView, ViewGroup parent) {
		View view = convertView;
		if(convertView == null || convertView.getId() != R.id.list_child_first) {
			LayoutInflater inflater = getInflater();
			view = inflater.inflate(R.layout.list_child_first, parent, false);
		}
		
		String startTime = timeFormat.format(lecture.getStart().getTime());
		String endTime = timeFormat.format(lecture.getEnd().getTime());
		
		ImageView image = (ImageView)view.findViewById(R.id.list_child_subject_indicator);
		image.setColorFilter(getColorForType(lecture.getType()), Mode.SRC_OVER);
		bind(view, R.id.list_child_subject_name, lecture.getName());
		bind(view, R.id.list_child_subject_time, startTime + " - " + endTime);
		bind(view, R.id.list_child_subject_location, lecture.getLocation());
		view.setClickable(true);
		return view;
	}

	public View getSingleChildView(Lecture lecture, View convertView, ViewGroup parent) {
		View view = convertView;
		if(convertView == null || convertView.getId() != R.id.list_child_single) {
			LayoutInflater inflater = getInflater();
			view = inflater.inflate(R.layout.list_child_single, parent, false);
		}
		
		String startTime = timeFormat.format(lecture.getStart().getTime());
		String endTime = timeFormat.format(lecture.getEnd().getTime());

		ImageView image = (ImageView)view.findViewById(R.id.list_child_subject_indicator);
		image.setColorFilter(getColorForType(lecture.getType()), Mode.SRC_OVER);
		bind(view, R.id.list_child_subject_name, lecture.getName());
		bind(view, R.id.list_child_subject_time, startTime + " - " + endTime);
		bind(view, R.id.list_child_subject_location, lecture.getLocation());
		view.setClickable(true);
		return view;
	}
	
	public int getColorForType(String type) {
		int fullColor = Color.GRAY;
		if(type.equals("HC")) {
			fullColor = Color.GREEN;
		} else if(type.equals("WC")) {
			fullColor = Color.BLUE;
		} else if(type.equals("TENT")) {
			fullColor = Color.RED;
		}
		
		return (0xFF000000 & TRANSPARANCY_LEVEL) | (0x00FFFFFF & fullColor);
	}

	public static void bind(View view, CharSequence item) {
		if(view != null && view instanceof TextView) {
			TextView txtView = (TextView)view;
			txtView.setText(item);
		}
	}
	
	public static void bind(View view, int resourceId) {
		if(view != null && view instanceof TextView) {
			TextView txtView = (TextView)view;
			txtView.setText(resourceId);
		}
	}
	
	public static void bind(View view, int viewId, CharSequence item) {
		View found = view.findViewById(viewId);
		if(found != null && found instanceof TextView) {
			TextView txtView = (TextView)found;
			txtView.setText(item);
		}
	}
	
	public static void bind(View view, int viewId, int resourceId) {
		View found = view.findViewById(viewId);
		if(found != null && found instanceof TextView) {
			TextView txtView = (TextView)found;
			txtView.setText(resourceId);
		}
	}
}
